package com.ug_project.dialog

import android.content.Context
import android.support.v4.app.Fragment
import com.ug_project.adapters.createBaseAdapter
import com.ug_project.objects.Tuple2.Tuple
import com.ug_project.objects.toArrayList
import com.ug_project.views.R
import com.ug_project.views.click
import com.ug_project.views.findListView
import com.ug_project.views.findTextView
import com.yurqi.dialog.SDialog

/**
 * Created by yeganaaa on 4/16/2017.
 */
fun <T> Context.dialogList(title: String = "", array: Array<T>, toString: (item: T) -> String =  {it.toString()},action: (param: Tuple<Int, T>) -> Unit){
    SDialog(this, R.layout.dialog_list).apply {
        val List = rootView.findListView(R.id.List)
        rootView.findTextView(R.id.Title).text = title
        List.adapter = createBaseAdapter(this@dialogList, R.layout.listitem, array.toArrayList())
        {
            GetViewIData { position, rootView, parent, data ->
                rootView.findTextView(R.id.Name).text = toString(data)
                rootView.click {
                    action(Tuple(position, data))
                    dismiss()
                }
            }
        }
    }.show()
}

fun <T> Fragment.dialogList(title: String = "", array: Array<T>, toString: (item: T) -> String =  {it.toString()},action: (param: Tuple<Int, T>) -> Unit){
    context.dialogList(title, array, toString, action)
}
